import java.util.Stack;

public class LeetCode3 {
    public int lengthOfLongestSubstring(String s) {
        int length = 0;


        if (s.length() > 0){
            length = 1;
            char[] chars = s.toCharArray();
            int[] temps = new int[128];
            int tempsIndex = 0;
            for (int k = 0; k < 128; k++){
                temps[k] = -1;
            }

            int charIndex = 0;
            for (int i = 0; i < chars.length; i++){
                int containsIndex = contains(temps, chars[i]);
                if ( containsIndex != -1){
                    //如果元素存在于临时数组中，则获取临时数组中存储数据的长度，作为当前最大的长度
                    length = Math.max(length, tempsIndex);

                    //一旦新位置的元素和临时数组中的数据有匹配的，则我们将字符串下滑一位重新计算最
                    i = charIndex;
                    charIndex++;

                    for (int size = 0; size <= tempsIndex; size++){
                        temps[size] = -1;
                    }
                    tempsIndex = 0;

                }else{
                    temps[tempsIndex] = chars[i];
                    tempsIndex++;
                }
            }

            length = Math.max(length, tempsIndex);
        }
        return length;
    }

    /**
     * 返回元素在临时数组中的位置
     * @param arr
     * @param empty
     * @return
     */
    public int contains(int[] arr, int empty){
        for (int i = 0; i < arr.length; i++){
            if (empty == arr[i]){
                return i;
            }
        }
        return -1;
    }

    private void printArr(int[] arr){
        System.out.print("打印数组数据：");
        for (int size = 0; size < arr.length; size++){
            if (arr[size] != -1){
                System.out.print( (char)arr[size]);
            }

        }
        System.out.println();
    }
}
